<?


// INCLUDES //
require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";

// user authentication
user_auth();

require_once SITE_ROOT_PATH."/_lib/classes/class.newsletter.php";


// HEAD //
require_once SITE_ROOT_PATH."/_lib/html/control_head.php";



// FUNCTION // Display home ======================
function home(){
?>


<h2>Email Entry</h2>

<? if(isset($GLOBALS['msg'])) echo "<div class='note'><p>".$GLOBALS['msg']."</p></div>"; ?>

<form method='POST' action='<?= $_SERVER['PHP_SELF']?>'>
	
	<input type="hidden" name="action" value="insert">

	<table>
	<tr>
	<td><label>Email addresses</label>&nbsp;&nbsp;</td>
	<td><textarea name="emails" cols="50" rows="10"></textarea></td>
	</tr>
	</table>

	<input name='Submit' value='Submit' type='submit'>
	
</form>







<?
}
// END FUNCTION ================================




// FUNCTION // Display reports ======================
function dealer_report(){

$stats = new stats();
$users = new users();
$month = $_REQUEST['dates_year']."-".$_REQUEST['dates_month'];


?>

<h2>View reports</h2>


<h3>Search date: <?= $month; ?></h3>

<table class="libra">

<tr bgcolor="#eeeeee">
	<th>ID</th>
	<th>Dealer</th>
	<th>Search Results</th>
	<th>Details Page Impressions</th>
	<th>Website Referals</th>
</tr>
<?

	//$details = $stats->get_report($month);
	$details = $users->get_all_users('0');
	
	$xls = $xls."ID,Dealer,Number of search results,Number page impression,Number of referals\n";	
	$i = 0;

	foreach($details as $value) {
	
		// highlight rows
		if($i % 2){
			$highlight = "style='background:#ececec'";
		}else{
			$highlight = "";
		}
	
?>
<tr <?= $highlight ?>>
	<td align="left"><?= $value ['user_id'] ?></td>
	<td align="left"><?= $value['dealer_name'] ?></td>
	<td align="center"><?= $stats->get_stats_results($month,$value['user_id']); ?></td>
	<td align="center"><?= $stats->get_stats_details($month,$value['user_id']); ?></td>
	<td align="center"><?= $stats->get_stats_referals($month,$value['user_id']); ?></td>
</tr>
<?

	/*
$xls .= $value['user_id'].",".$value['dealer_name'].",".$value['rcounter'].",".$value['dcounter'].",".$value['ucounter']."\n";


	srand(time());
	$rval = 1000 + (rand() % 10000);
	$strFile = "stats".$rval.".csv";
	
	if(file_exists($strFile)) unlink($strFile);
	
	if($file = fopen($strFile,"w") OR die()){
		fputs($file,$xls);
	}else{
		$strFile = "";
	}

	*/


$i++;

}
?>
</table>


<?


}
// END FUNCTION ================================






switch($_REQUEST['action']){
	default;
		home();
	break;
	case "insert";
		$newsletter = new newsletter();
		$total = $newsletter->insert_bulk_email($_POST['emails']);
		$GLOBALS['msg'] = $total." email addresses added.";
		home();
	break;
}

	
require_once SITE_ROOT_PATH."/_lib/html/control_foot.php";
	
?>